package com.rc.common.encoder;

import com.rc.common.model.MessageModel;
import com.rc.common.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;

import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;

/**
 *将websocket发送的string 转换为 messageModel
 */
public class MessageDecoder implements Decoder.Text<MessageModel> {
    @Override
    public MessageModel decode(String s) throws DecodeException {
        return JsonUtil.toObj(s, MessageModel.class);
    }

    //确定是否可以编码
    @Override
    public boolean willDecode(String s) {
        return  StringUtils.startsWith(s,"{") && StringUtils.endsWith(s,"}");
    }

    @Override
    public void init(EndpointConfig endpointConfig) {

    }

    @Override
    public void destroy() {
    }
}
